package com;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.game.ServerGame;
import com.game.ServerGameAPI;
import com.game.ServerGameSettings;

/**
 * User: wattsc5
 * <p/>
 * Date: 2/26/12
 */
public class ServerMain
{
	private static final Logger logger = Logger.getLogger(ServerMain.class);

	/**
	 * Initializes a new instance of a com.ServerMain.
	 */
	public static void main(String[] args)
	{
		int port = 8888;

		ServerGameSettings gameSettings = new ServerGameSettings();

		// Check for args
		for (int i = 0; i < args.length; i++)
		{
			// Enable Verbose mode.
			if (args[i].equals("-v"))
			{
				Logger.getRootLogger().setLevel(Level.ALL);
				logger.info("Verbose mode enabled");
			}
			else if (args[i].equals("-p"))
			{
				// Handle port change
				port = Integer.parseInt(args[i + 1]);
				i++;
			}
			else if (args[i].equals("-m"))
			{
				// Handle Min number of players change
				gameSettings.setMinNumberOfPlayers(Integer.parseInt(args[i + 1]));
				i++;
			}
			else if (args[i].equals("-M"))
			{
				// Handle Max number of players change
				gameSettings.setMaxNumberOfPlayers(Integer.parseInt(args[i + 1]));
				i++;
			}
			else if (args[i].equals("-t"))
			{
				// Handle Time to start round change
				gameSettings.setWaitTimeToStart(Integer.parseInt(args[i + 1]));
				i++;
			}
			else if (args[i].equals("-a"))
			{
				// Handle Move attempt count change
				gameSettings.setAttemptMoveCount(Integer.parseInt(args[i + 1]));
				i++;
			}
			else if (args[i].equals("-T"))
			{
				// Handle Move attempt count change
				gameSettings.setTurnTimeout(Integer.parseInt(args[i + 1]));
				i++;
			}
			else if (args[i].equals("-d"))
			{
				// Handle Move attempt count change
				gameSettings.setStartingNumberOfDie(Integer.parseInt(args[i + 1]));
				i++;
			}
		}

		logger.debug("Port set to " + port);
		logger.debug("Min number of players set to " + gameSettings.getMinNumberOfPlayers());
		logger.debug("Max number of players set to " + gameSettings.getMaxNumberOfPlayers());
		logger.debug("Time to start round set to " + gameSettings.getWaitTimeToStart());
		logger.debug("Move attempt count set to " + gameSettings.getAttemptMoveCount());
		logger.debug("Turn timeout set to " + gameSettings.getTurnTimeout());
		logger.debug("Number of starting dice " + gameSettings.getStartingNumberOfDie());

		ServerGame game = new ServerGame(gameSettings);
		ServerGameAPI api = new ServerGameAPI(game);

		logger.info("Server starting on port " + port);
		game.initializeGame(api, port);
	}
}
